import { defineStore } from 'pinia'
import { ref } from 'vue';
import { api } from '@/mock/mockApi';
import { resetRouter } from '@/router/index'
import { getTokenCookie, setTokenCookie, removeTokenCookie } from '@/utils/auth/index'

const useInfoStore = defineStore('use', () => {

    // 用户名称
    const useInfo = ref({});
    // 模拟跳转(可删)
    const usrName = ref('')
    // 设置用户信息
    const setUseInfo = (val) => {
        useInfo.value = val;
    }
    // 获取用户信息
    const getUseInfo = () => {
        return new Promise((resolve, reject) => {
            api({
                name: '超级管理员',
                age: 28,
                company: '国产数统工具总部研发中心',
                roles: usrName.value === 'admin' ? [ // (可删)
                    'simpleGraph',
                    'basicShapes',
                    'customNodeStyles',
                    'sprite',
                    'virtualList',
                    'jsx',
                    'jsx-basic',
                    'jsx-taskComponents',
                    'vue-data-ui',
                    'vue-data-ui-basic',
                    'lowCode-customForm'
                ] : [
                    'simpleGraph',
                    'basicShapes',
                    'customNodeStyles',
                    'sprite',
                    'virtualList',
                    'jsx',
                    'jsx-basic',
                    'jsx-taskComponents',
                    'vue-data-ui',
                    'vue-data-ui-basic',
                    'lowCode-customForm'
                ]
            }).then(res => {
                if (res.code === 200) {
                    setUseInfo(res.data);
                    resolve(res.data)
                }
            }).catch(error => {
                reject(error)
            })
        })
    }
    // 移除用户信息
    const removeUseInfo = () => {
        useInfo.value = {};
    }

    // token
    const token = ref(getTokenCookie()); // 02-初始化读取值,设置直接获取本地达到长缓存的效果
    // 设置token
    const setToken = (val) => {
        token.value = val;
        // 01-设置token长缓存
        setTokenCookie(val);
    }
    // 获取token
    const getSetToken = async (name) => {
        usrName.value = name; // (可删)
        // 这里是模拟token
        return new Promise((resolve, reject) => {
            api("token123456789").then(res => {
                if (res.code === 200) {
                    setToken(res.data);
                    resolve()
                }
            }).catch(error => {
                reject(error)
            })
        })


    }
    // 清除token
    const removeSetToken = async () => {
        token.value = null;
        removeTokenCookie();
    }

    // 退出登录
    const loginOut = () => {
        return new Promise((resolve, reject) => {
            api({ msg: "退出登录成功" }).then(() => {
                // 01-清除token长缓存
                removeSetToken();

                // 02-清除用户信息
                removeUseInfo();

                // 03-重置路由实例
                resetRouter();

                resolve()
            }).catch(error => {
                reject(error)
            })
        })


    }

    return {
        useInfo,
        setUseInfo,
        getUseInfo,
        removeUseInfo,
        token,
        setToken,
        getSetToken,
        removeSetToken,
        loginOut
    }
})

export default useInfoStore;